package com.xinzhiduming.mp3palyer;

import java.io.File;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class PlaySongListViewAdapter extends BaseAdapter {

	ArrayList<File> mPlaySongs = new ArrayList<File>();
	Context mContext = null;
	Boolean isEmptyFolder = false;
	LayoutInflater mLayoutInflater = null;

	PlaySongListViewAdapter(ArrayList<File> sonsList, Context context) {
		if (sonsList == null) {
			isEmptyFolder = true;
		} else {
			mPlaySongs = sonsList;
		}
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (isEmptyFolder) {
			return 0;
		}
		return mPlaySongs.size();
	}

	@Override
	public File getItem(int position) {
		// TODO Auto-generated method stub
		if (isEmptyFolder) {
			return null;
		}
		return mPlaySongs.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		if (isEmptyFolder) {
			return 0;
		}
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (isEmptyFolder) {
			return null;
		}
		_ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new _ViewHolder();
			convertView = mLayoutInflater.inflate(R.layout.play_song_listview_detail, null);
			viewHolder.songListTv = (TextView) convertView.findViewById(R.id.song_name);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (_ViewHolder) convertView.getTag();
		}
		String lastSongNameString = DatabaseOperator.getData(AppConstant.LAST_TIME_PALYED_SONG, mContext);
		if (lastSongNameString != null && !lastSongNameString.equals("")) {
			File file = new File(lastSongNameString);
			if (file != null && file.exists() && mPlaySongs.contains(file) && position == mPlaySongs.indexOf(file)) {
				viewHolder.songListTv.setTextColor(android.graphics.Color.CYAN);
			} else {
				viewHolder.songListTv.setTextColor(android.graphics.Color.GREEN);
			}
		} else {
			viewHolder.songListTv.setTextColor(android.graphics.Color.GREEN);
		}
		viewHolder.songListTv.setText(getItem(position).getName());
		return convertView;
	}

	public static class _ViewHolder {
		public TextView songListTv;
	}
}